package com.fw.leetcode.doublepointer;

import com.fw.leetcode.LeetCode;

import java.util.Arrays;

/**
 * 31. Next Permutation
 *
 * A permutation of an array of integers is an arrangement of its members into a sequence or linear order.
 *
 * · For example, for arr = [1,2,3], the following are all the permutations of arr:
 *   [1,2,3], [1,3,2], [2, 1, 3], [2, 3, 1], [3,1,2], [3,2,1].
 *
 * The next permutation of an array of integers is the next lexicographically greater permutation of its integer.
 * More formally, if all the permutations of the array are sorted in one container according to their
 * lexicographical order, then the next permutation of that array is the permutation that follows
 * it in the sorted container. If such arrangement is not possible, the array must be rearranged as the
 * lowest possible order (i.e., sorted in ascending order).
 *
 * · For example, the next permutation of arr = [1,2,3] is [1,3,2].
 * · Similarly, the next permutation of arr = [2,3,1] is [3,1,2].
 * · While the next permutation of arr = [3,2,1] is [1,2,3] because [3,2,1] does not have a lexicographical larger rearrangement.
 *
 * Given an array of integers nums, find the next permutation of nums.
 * The replacement must be in place and use only constant extra memory.
 *
 * Example 1:
 *  Input: nums = [1,2,3]
 *  Output: [1,3,2]
 *
 * Example 2:
 *  Input: nums = [3,2,1]
 *  Output: [1,2,3]
 *
 * Example 3:
 *  Input: nums = [1,1,5]
 *  Output: [1,5,1]
 *
 * Constraints:
 *  1 <= nums.length <= 100
 *  0 <= nums[i] <= 100
 */
public class Num_0031 implements LeetCode {
    private interface Solution {
        void nextPermutation(int[] nums);

        default void assertEquals(int[] nums, int[] expected) {
            int[] oldNums = new int[nums.length];
            System.arraycopy(nums, 0, oldNums, 0, nums.length);
            nextPermutation(nums);
            if (!Arrays.equals(nums, expected)) {
                nextPermutation(oldNums);
            }
        }
    }

    private static class MySolution implements Solution {

        @Override
        public void nextPermutation(int[] nums) { // 双指针：时O(n) 空O(1)
            /*
             * 下一个全排列（原位更新）
             * [1,2,3] -> [1,3,2] -> [2,1,3] -> [2,3,1] -> [3,1,2] -> [3,2,1] -> [1,2,3]
             * [1,1,5] -> [1,5,1] -> [5,1,1] -> [1,1,5]
             * [1,2,3,4] -> [1,2,4,3] -> [1,3,2,4]
             * 步骤：
             * 1.从右往左(0 <- n-1)寻找第一个升序的数a[i]
             * 若没有找到，直接跳至第4步
             *  2.从右往左(0 <- n-1)寻找第一个大于a[i]的数a[j]
             *  3.交换两数a[i]和a[j]位置
             * 4.反转(i+1, n-1) <= [!] 注：边界不是(i+1, j)
             */
            // 1.寻找第一个升序的数a[i]
            int i = nums.length - 2;
            while (i >= 0 && nums[i] >= nums[i+1]) {
                i--;
            }
            if (i >= 0) {
                // 2.寻找第一个大于a[i]的数a[j]
                int j = nums.length - 1;
                while (j >= 0 && nums[j] <= nums[i]) {
                    j--;
                }
                // 3.交换a[i]和a[j]位置
                swap(nums, i, j);
            }
            // 4.反转
            reverse(nums, i+1);
        }

        private void swap(int[] nums, int i, int j) {
            int tmp = nums[i];
            nums[i] = nums[j];
            nums[j] = tmp;
        }

        private void reverse(int[] nums, int start) { // 左右指针相向靠近
            int left = start;
            int right = nums.length - 1;
            while (left < right) {
                swap(nums, left, right);
                left++;
                right--;
            }
        }
    }

    public static void main(String[] args) {
        Solution solution = new MySolution();
        solution.assertEquals(new int[]{1,2,3,4}, new int[]{1,2,4,3});
        solution.assertEquals(new int[]{1,2,3}, new int[]{1,3,2});
        solution.assertEquals(new int[]{1,3,2}, new int[]{2,1,3});
        solution.assertEquals(new int[]{2,1,3}, new int[]{2,3,1});
        solution.assertEquals(new int[]{2,3,1}, new int[]{3,1,2});
        solution.assertEquals(new int[]{3,1,2}, new int[]{3,2,1});
        solution.assertEquals(new int[]{3,2,1}, new int[]{1,2,3});
        solution.assertEquals(new int[]{1,1,5}, new int[]{1,5,1});
        solution.assertEquals(new int[]{1,5,1}, new int[]{5,1,1});
        solution.assertEquals(new int[]{5,1,1}, new int[]{1,1,5});
        solution.assertEquals(new int[]{1}, new int[]{1});
        solution.assertEquals(new int[]{1,2}, new int[]{2,1});
        solution.assertEquals(new int[]{2,1}, new int[]{1,2});
    }
}
